* s~\t+$~~
[lhc/web/wiklou.git] / includes / Database.php
index 8fc4b27..bfc8d10 100644 (file)
@@ -62,6 +62,7 @@ class Database {
        var $mFlags;
        var $mTrxLevel = 0;
        var $mErrorCount = 0;
+       var $mLBInfo = array();
        /**#@-*/
 
 #------------------------------------------------------------------------------
@@ -130,6 +131,29 @@ class Database {
                return wfSetVar( $this->mErrorCount, $count );
        }
 
+       /**
+        * Properties passed down from the server info array of the load balancer
+        */
+       function getLBInfo( $name = NULL ) {
+               if ( is_null( $name ) ) {
+                       return $this->mLBInfo;
+               } else {
+                       if ( array_key_exists( $name, $this->mLBInfo ) ) {
+                               return $this->mLBInfo[$name];
+                       } else {
+                               return NULL;
+                       }
+               }
+       }
+
+       function setLBInfo( $name, $value = NULL ) {
+               if ( is_null( $value ) ) {
+                       $this->mLBInfo = $name;
+               } else {
+                       $this->mLBInfo[$name] = $value;
+               }
+       }
+
        /**#@+
         * Get function
         */
@@ -221,6 +245,8 @@ class Database {
         * If the failFunction is set to a non-zero integer, returns success
         */
        function open( $server, $user, $password, $dbName ) {
+               global $wguname;
+
                # Test for missing mysql.so
                # First try to load it
                if (!@extension_loaded('mysql')) {
@@ -258,7 +284,9 @@ class Database {
                        if ( $this->mConn !== false ) {
                                $success = @/**/mysql_select_db( $dbName, $this->mConn );
                                if ( !$success ) {
-                                       wfDebug( "Error selecting database \"$dbName\": " . $this->lastError() . "\n" );
+                                       $error = "Error selecting database $dbName on server {$this->mServer} " .
+                                               "from client host {$wguname['nodename']}\n";
+                                       wfDebug( $error );
                                }
                        } else {
                                wfDebug( "DB connection error\n" );
@@ -274,6 +302,14 @@ class Database {
                if ( !$success ) {
                        $this->reportConnectionError();
                }
+
+               global $wgDBmysql5;
+               if( $wgDBmysql5 ) {
+                       // Tell the server we're communicating with it in UTF-8.
+                       // This may engage various charset conversions.
+                       $this->query( 'SET NAMES utf8' );
+               }
+
                $this->mOpened = $success;
                return $success;
        }
@@ -300,16 +336,21 @@ class Database {
 
        /**
         * @access private
-        * @param string $msg error message ?
+        * @param string $error fallback error message, used if none is given by MySQL
         */
-       function reportConnectionError() {
+       function reportConnectionError( $error = 'Unknown error' ) {
+               $myError = $this->lastError();
+               if ( $myError ) {
+                       $error = $myError;
+               }
+
                if ( $this->mFailFunction ) {
                        if ( !is_int( $this->mFailFunction ) ) {
                                $ff = $this->mFailFunction;
-                               $ff( $this, $this->lastError() );
+                               $ff( $this, $error );
                        }
                } else {
-                       wfEmergencyAbort( $this, $this->lastError() );
+                       wfEmergencyAbort( $this, $error );
                }
        }
 
@@ -334,7 +375,11 @@ class Database {
                if ( $wgProfiling ) {
                        # generalizeSQL will probably cut down the query to reasonable
                        # logging size most of the time. The substr is really just a sanity check.
-                       $profName = 'query: ' . $fname . ' ' . substr( Database::generalizeSQL( $sql ), 0, 255 ); 
+
+                       # Who's been wasting my precious column space? -- TS
+                       #$profName = 'query: ' . $fname . ' ' . substr( Database::generalizeSQL( $sql ), 0, 255 );
+                       $profName = 'query: ' . substr( Database::generalizeSQL( $sql ), 0, 255 );
+
                        wfProfileIn( 'Database::query' );
                        wfProfileIn( $profName );
                }
@@ -343,7 +388,7 @@ class Database {
 
                # Add a comment for easy SHOW PROCESSLIST interpretation
                if ( $fname ) {
-                       $commentedSql = "/* $fname */ $sql";
+                       $commentedSql = preg_replace("/\s/", " /* $fname */ ", $sql, 1);
                } else {
                        $commentedSql = $sql;
                }
@@ -707,7 +752,7 @@ class Database {
                        $tailOpts .= " ORDER BY {$options['ORDER BY']}";
                }
                if (isset($options['LIMIT'])) {
-                       $tailOpts .= $this->limitResult('', $options['LIMIT'], 
+                       $tailOpts .= $this->limitResult('', $options['LIMIT'],
                                isset($options['OFFSET']) ? $options['OFFSET'] : false);
                }
                if ( is_numeric( array_search( 'FOR UPDATE', $options ) ) ) {
@@ -993,11 +1038,11 @@ class Database {
                $opts = array();
                if ( in_array( 'LOW_PRIORITY', $options ) )
                        $opts[] = $this->lowPriorityOption();
-               if ( in_array( 'IGNORE', $options ) ) 
+               if ( in_array( 'IGNORE', $options ) )
                        $opts[] = 'IGNORE';
                return implode(' ', $opts);
        }
-       
+
        /**
         * UPDATE wrapper, takes a condition array and a SET array
         *
@@ -1024,7 +1069,7 @@ class Database {
         * $mode: LIST_COMMA         - comma separated, no field names
         *        LIST_AND           - ANDed WHERE clause (without the WHERE)
         *        LIST_SET           - comma separated with field names, like a SET clause
-        *        LIST_NAMES         - comma separated field names
+        *        LIST_NAMES         - comma separated field names
         */
        function makeList( $a, $mode = LIST_COMMA ) {
                if ( !is_array( $a ) ) {
@@ -1181,7 +1226,7 @@ class Database {
                $s=str_replace(array('%','_'),array('\%','\_'),$s);
                return $s;
        }
-               
+
        /**
         * Returns an appropriately quoted sequence value for inserting a new row.
         * MySQL has autoincrement fields, so this is just NULL. But the PostgreSQL
@@ -1593,8 +1638,12 @@ class Database {
                while ( $row = $this->fetchObject( $res ) ) {
                        if ( $row->User == 'system user' ) {
                                if ( ++$slaveThreads == 2 ) {
-                                       # This is it, return the time
-                                       return $row->Time;
+                                       # This is it, return the time (except -ve)
+                                       if ( $row->Time > 1>>31 ) {
+                                               return 0;
+                                       } else {
+                                               return $row->Time;
+                                       }
                                }
                        }
                }
@@ -1623,13 +1672,6 @@ class Database {
        function encodeBlob($b) {
                return $b;
        }
-
-       function notNullTimestamp() {
-               return "!= 0";
-       }
-       function isNullTimestamp() {
-               return "= '0'";
-       }
 }
 
 /**
@@ -1655,7 +1697,7 @@ class ResultWrapper {
        /**
         * @todo document
         */
-       function ResultWrapper( $database, $result ) {
+       function ResultWrapper( &$database, $result ) {
                $this->db =& $database;
                $this->result =& $result;
        }
@@ -1752,12 +1794,15 @@ border=\"0\" ALT=\"Google\"></A>
        if ( is_object( $wgMessageCache ) ) {
                $wgMessageCache->disable();
        }
-       
-       $msg = wfGetSiteNotice();
-       if($msg == '') {
-               $msg = str_replace( '$1', $error, $noconnect );
+
+       if ( trim( $error ) == '' ) {
+               $error = $this->mServer;
        }
-       $text = $msg;
+
+       wfLogDBError( "Connection error: $error\n" );
+
+       $text = str_replace( '$1', $error, $noconnect );
+       $text .= wfGetSiteNotice();
 
        if($wgUseFileCache) {
                if($wgTitle) {